package lesson.exam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lesson.base.BaseService;
import lesson.db.DbUtil;
import lesson.grade.Grade;
import lesson.subject.SubjectDao;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ExamService extends BaseService {


    public String addExam(Exam exam, List<ExamSubject> subjects) {
        if (isOk(exam.getName()) == false) {
            return "请填写考试名称";
        }
        if (exam.getStartDate() == null || exam.getEndDate() == null) {
            return "请填写考试日期";
        }

        if (exam.getEndDate().before(exam.getStartDate())) {
            return "结束日期不能早于开始日期";
        }
        if (subjects == null || subjects.size() == 0) {
            return "请选择要考试的科目";
        }
        SqlSession session = DbUtil.getSession(false);

        ExamDao examDao = session.getMapper(ExamDao.class);
        examDao.addExam(exam);
        for (ExamSubject subject : subjects) {
            subject.setExamId(exam.getId());
        }
        ExamSubjectDao examSubjectDao = session.getMapper(ExamSubjectDao.class);
        examSubjectDao.batchAdd(subjects);


        session.commit();
        session.close();


        return null;
    }

    /**
     * 添加考试
     *
     * @param exam
     * @param subjects
     * @return
     */
    public String updateExam(Exam exam, List<ExamSubject> subjects) {
        if (isOk(exam.getName()) == false) {
            return "请填写考试名称";
        }
        if (exam.getStartDate() == null || exam.getEndDate() == null) {
            return "请填写考试日期";
        }

        if (exam.getEndDate().before(exam.getStartDate())) {
            return "结束日期不能早于开始日期";
        }
        if (subjects == null || subjects.size() == 0) {
            return "请选择要考试的科目";
        }
        SqlSession session = DbUtil.getSession(false);

        ExamDao examDao = session.getMapper(ExamDao.class);
        examDao.updateExam(exam);

        ExamSubjectDao examSubjectDao = session.getMapper(ExamSubjectDao.class);
        examSubjectDao.delByExam(exam.getId());

        for (ExamSubject subject : subjects) {
            subject.setExamId(exam.getId());
        }
        examSubjectDao.batchAdd(subjects);


        session.commit();
        session.close();


        return null;
    }

    public List<Exam> listAllExam() {
        SqlSession session = DbUtil.getSession(true);
        try {
            ExamDao dao = session.getMapper(ExamDao.class);
            List<Exam> list = dao.list(null);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }

    }

    /**
     * 保存考试
     */
    public String saveExam(Exam exam, List<ExamSubject> subjects) {
        if (exam.getId() == null) {
            return addExam(exam, subjects);
        } else {
            return updateExam(exam, subjects);
        }


    }


    /**
     * 检测叫这个名字的用户是否存在
     *
     * @param name
     * @return
     */
    public boolean checkExist(Integer exceptId, String name) {
        SqlSession session = DbUtil.getSession(true);
        SubjectDao subjectDao = session.getMapper(SubjectDao.class);
        boolean exist = subjectDao.checkExist(exceptId, name);
        session.close();
        return exist;
    }

    /**
     * 根据关键字查询
     *
     * @param keywords
     * @return
     */
    public PageInfo<Exam> list(String keywords, Integer pageNumber, Integer pageSize) {
        SqlSession session = DbUtil.getSession(true);
        ExamDao examDao = session.getMapper(ExamDao.class);
        PageHelper.startPage(pageNumber, pageSize);
        List<Exam> list = examDao.list(keywords);

        Map<Integer, Exam> map = new HashMap<>();


        if (list.isEmpty() == false) {
            Integer[] examIds = new Integer[list.size()];
            for (int i = 0; i < list.size(); i++) {
                examIds[i] = list.get(i).getId();
                map.put(list.get(i).getId(), list.get(i));

            }

            ExamSubjectDao subjectDao = session.getMapper(ExamSubjectDao.class);
            List<ExamSubject> subjects = subjectDao.list(examIds);


            for (ExamSubject subject : subjects) {
                Exam exam = map.get(subject.getExamId());
                if (exam.getSubjectGroup() == null) {
                    exam.setSubjectGroup(new LinkedHashMap<>());
                }
                //{}
                //{一年级: 语文}
                //{一年级 ： 语文,数学}
                String subjectNames = exam.getSubjectGroup().get(subject.getGradeName());
                if (isOk(subjectNames)) {
                    subjectNames += "," + subject.getSubjectName();
                } else {
                    subjectNames = subject.getSubjectName();
                }
                exam.getSubjectGroup().put(subject.getGradeName(), subjectNames);
            }

        }


        session.close();
        return new PageInfo<>(list);
    }


    public String del(Integer id) {
        SqlSession session = DbUtil.getSession(false);
        try {
            ExamDao examDao = session.getMapper(ExamDao.class);
            examDao.delExam(id);

            ExamSubjectDao subjectDao = session.getMapper(ExamSubjectDao.class);
            subjectDao.delByExam(id);

            session.commit();
        } finally {
            session.close();
        }

        return null;
    }

    public Exam getById(Integer id) {
        SqlSession session = DbUtil.getSession(true);
        ExamDao examDao = session.getMapper(ExamDao.class);
        Exam exam = examDao.getById(id);
        session.close();
        return exam;
    }

    /**
     * 列出某次考试的年级
     *
     * @param examId
     * @return
     */
    public List<Grade> listExamGrades(Integer examId) {
        SqlSession session = DbUtil.getSession(true);
        ExamSubjectDao dao = session.getMapper(ExamSubjectDao.class);
        List<Grade> grades = dao.listExamGrades(examId);

        session.close();
        return grades;
    }

    /**
     * 列出某次考试的学科
     *
     * @param examId
     * @return
     */
    public List<Grade> listExamSubjects(Integer examId, Integer gradeId) {
        SqlSession session = DbUtil.getSession(true);
        ExamSubjectDao dao = session.getMapper(ExamSubjectDao.class);
        List<Grade> grades = dao.listExamSubjects(examId, gradeId);

        session.close();
        return grades;
    }

}